import { Controller, Get, HttpException, Query } from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';
import { Public } from '../auth/decorators/auth.decorator';
import { RVo } from '../utils/result.vo';
import { SmsService } from './sms.service';

@Controller('sms')
export class SmsController {
  constructor(private readonly smsService: SmsService) {}

  @Get()
  @Public()
  @Throttle({ default: { ttl: 1 * 60 * 1000, limit: 1 } })
  async sendSms(@Query('phone') phone: string) {
    const phoneRegex = /^1\d{10}$/;
    if (typeof phone === 'string' && phoneRegex.test(phone)) {
      const data = await this.smsService.send(phone);
      return new RVo({ data });
    } else {
      throw new HttpException('手机号格式不对', 400);
    }
  }
}
